package com.app.newiptv.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.app.newiptv.R;
import com.app.newiptv.adaper.KeyboardAdapter;
import com.app.newiptv.adaper.SearchAdapter;
import com.app.newiptv.presenter.SearchPresenter;
import com.app.newiptv.utils.Paths;
import com.app.newiptv.view.FocusGridLayoutManager;
import com.app.newiptv.view.MyRecycleView;

public class SearchActivity extends Activity implements View.OnClickListener {
    private MyRecycleView mKeyboard_rv;
    private MyRecycleView mSearch_result_rv;
    private SearchPresenter mSearchPresenter;
    private RelativeLayout mPopup_key_view;
    private RelativeLayout mActivity_search;
    private EditText mSearch_edt;
    private String contentText = "";
    private ImageView mTo_search;
    private ImageView focus_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        initData();
        setLinsenter();
    }

    private void initView() {
        mKeyboard_rv = (MyRecycleView) findViewById(R.id.keyboard_rv);
        mSearch_result_rv = (MyRecycleView) findViewById(R.id.search_result_rv);
        mPopup_key_view = (RelativeLayout) findViewById(R.id.popup_key_view);
        mActivity_search = (RelativeLayout) findViewById(R.id.activity_search);
        mSearch_edt = (EditText) findViewById(R.id.search_edt);
        mTo_search = (ImageView) findViewById(R.id.to_search);
        focus_view = (ImageView) findViewById(R.id.focus_view);
    }

    private void initData() {
        mSearchPresenter = new SearchPresenter(this);
        mSearchPresenter.setView(mPopup_key_view, mActivity_search);
        mSearchPresenter.setDisplayed(true);
        mSearchPresenter.loadKeyboard(searchCallback);
        String hotUrl = Paths.HOSTPATH + Paths.CHANNLE_LIST + "?page=2";
        mSearchPresenter.loadItem(hotUrl, searchCallback);

    }

    private void setLinsenter() {
        mTo_search.setOnClickListener(this);
        mSearch_edt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                contentText = charSequence.toString();
                if (contentText != null && !contentText.isEmpty()) {
                    mSearchPresenter.loadItem(Paths.HOSTPATH + Paths.CHANNLE_LIST + "?wd=" + contentText, searchCallback);
                }else{
                    String hotUrl = Paths.HOSTPATH + Paths.CHANNLE_LIST + "?page=2";
                    mSearchPresenter.loadItem(hotUrl, searchCallback);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        mTo_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //Toast.makeText(SearchActivity.this, "选中", Toast.LENGTH_SHORT).show();
                } else {

                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mSearchPresenter.onKeyDown(keyCode, event);

    }

    private SearchPresenter.Callback searchCallback = new SearchPresenter.Callback() {
        @Override
        public void onError(int code, String msg) {

        }

        @Override
        public void onMovieCompleted(SearchPresenter presenter, int count) {
            SearchAdapter searchAdapter = new SearchAdapter(presenter,SearchActivity.this);
            searchAdapter.setCount(count);
            mSearch_result_rv.setLayoutManager(new FocusGridLayoutManager(SearchActivity.this, 2));
            mSearch_result_rv.setAdapter(searchAdapter);
        }

        @Override
        public void onKeyboardCompleted(SearchPresenter presenter, int count) {
            KeyboardAdapter keyboardAdapter = new KeyboardAdapter(presenter,focus_view);
            keyboardAdapter.setCount(count);
            mKeyboard_rv.setLayoutManager(new FocusGridLayoutManager(SearchActivity.this, 3));
            mKeyboard_rv.setAdapter(keyboardAdapter);
            mKeyboard_rv.post(new Runnable() {
                @Override
                public void run() {
                    if (mKeyboard_rv.getChildCount() > 0) {
                        mKeyboard_rv.getChildAt(0).requestFocus();
                    }
                }
            });
        }

        @Override
        public void onSearchCompleted(SearchPresenter presenter) {
            presenter.setSearchData(viewHolder);
        }
    };

    private SearchPresenter.ISetViewHolder viewHolder = new SearchPresenter.ISetViewHolder() {
        @Override
        public void setSearchEdt(String text, int type) {
            if (9 == type) {
                contentText = "";
                mSearch_edt.setText(contentText);
            } else if (11 == type) {
                if (contentText != null && !contentText.isEmpty()) {
                    contentText = contentText.substring(0, contentText.length() - 1);
                    mSearch_edt.setText(contentText);
                }
            } else {
                contentText += text;
                mSearch_edt.setText(contentText);
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.to_search:
                //Toast.makeText(SearchActivity.this, "请求", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
